package org.afun.oldhand.app;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.afun.oldhand.R;
import org.afun.oldhand.base.BaseFragment;
import org.afun.oldhand.forum.fragment.ForumFragment;
import org.afun.oldhand.home.fragment.HomeFragment;
import org.afun.oldhand.order.fragment.OrderFragment;
import org.afun.oldhand.user.fragment.UserFrament;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ViewPager2 viewPager;
    private LinearLayout llHome, llForum, llOrder, llProfile;
    private ImageView ivHome, ivForum, ivOrder, ivProfile, ivCurrent;

    private ArrayList<BaseFragment> fragments;
    private HomeFragment homeFrament;
    private ForumFragment forumFragment;
    private OrderFragment orderFragment;
    private UserFrament userFrament;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        setContentView(R.layout.activity_main);
        initPager();
        initTabView();
    }


    private void initTabView() {
        llHome = findViewById(R.id.id_tab_home);
        llHome.setOnClickListener(this);
        llForum = findViewById(R.id.id_tab_forum);
        llForum.setOnClickListener(this);
        llOrder = findViewById(R.id.id_tab_order);
        llOrder.setOnClickListener(this);
        llProfile = findViewById(R.id.id_tab_profile);
        llProfile.setOnClickListener(this);

        ivHome = findViewById(R.id.tab_iv_home);
        ivForum = findViewById(R.id.tab_iv_forum);
        ivOrder = findViewById(R.id.tab_iv_order);
        ivProfile = findViewById(R.id.tab_iv_profile);

        ivHome.setSelected(true);
        ivCurrent = ivHome;

    }

    private void initPager() {
        viewPager = findViewById(R.id.id_viewpager);
        fragments = new ArrayList<>();
        homeFrament = new HomeFragment();
        forumFragment = new ForumFragment();
        orderFragment = new OrderFragment();
        userFrament = new UserFrament();

        fragments.add(homeFrament);
        fragments.add(forumFragment);
        fragments.add(orderFragment);
        fragments.add(userFrament);


        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        switch (position) {
            case R.id.id_tab_home:
                viewPager.setCurrentItem(0);
            case 0:
                ivHome.setSelected(true);
                ivCurrent = ivHome;
                break;
            case R.id.id_tab_forum:
                viewPager.setCurrentItem(1);
            case 1:
                ivForum.setSelected(true);
                ivCurrent = ivForum;
                break;
            case R.id.id_tab_order:
                viewPager.setCurrentItem(2);
            case 2:
                ivOrder.setSelected(true);
                ivCurrent = ivOrder;
                break;
            case R.id.id_tab_profile:
                viewPager.setCurrentItem(3);
            case 3:
                ivProfile.setSelected(true);
                ivCurrent = ivProfile;
                break;

        }
    }

    @Override
    public void onClick(View view) {
        changeTab(view.getId());
    }
}